{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import quandl\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.formula.api as sm\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.stats.mstats import normaltest\n",
    "import time\n",
    "from cvxopt import matrix\n",
    "import seaborn as sns\n",
    "import statsmodels.tsa.stattools as ts\n",
    "from scipy import stats\n",
    "from scipy.optimize import minimize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "fama_5 = quandl.get('KFRENCH/FACTORS5_D')\n",
    "fama_5 = fama_5/100\n",
    "fama_5 = fama_5.rename(columns = {'Mkt-RF':'mkt'})\n",
    "spy = quandl.get('LSE/SPY5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "spy = np.log(spy['Last Close']).diff().dropna()\n",
    "spy.name = 'spy'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mkt</th>\n",
       "      <th>SMB</th>\n",
       "      <th>HML</th>\n",
       "      <th>RMW</th>\n",
       "      <th>CMA</th>\n",
       "      <th>RF</th>\n",
       "      <th>spy</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-06-23</th>\n",
       "      <td>0.0024</td>\n",
       "      <td>0.0077</td>\n",
       "      <td>-0.0050</td>\n",
       "      <td>-0.0035</td>\n",
       "      <td>0.0005</td>\n",
       "      <td>0.00003</td>\n",
       "      <td>-0.000615</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-26</th>\n",
       "      <td>0.0004</td>\n",
       "      <td>0.0005</td>\n",
       "      <td>0.0069</td>\n",
       "      <td>0.0025</td>\n",
       "      <td>0.0017</td>\n",
       "      <td>0.00003</td>\n",
       "      <td>0.000738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-27</th>\n",
       "      <td>-0.0084</td>\n",
       "      <td>-0.0025</td>\n",
       "      <td>0.0130</td>\n",
       "      <td>0.0031</td>\n",
       "      <td>0.0032</td>\n",
       "      <td>0.00003</td>\n",
       "      <td>-0.000779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-28</th>\n",
       "      <td>0.0102</td>\n",
       "      <td>0.0082</td>\n",
       "      <td>0.0021</td>\n",
       "      <td>-0.0018</td>\n",
       "      <td>-0.0007</td>\n",
       "      <td>0.00003</td>\n",
       "      <td>-0.000205</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-29</th>\n",
       "      <td>-0.0083</td>\n",
       "      <td>0.0007</td>\n",
       "      <td>0.0135</td>\n",
       "      <td>-0.0025</td>\n",
       "      <td>0.0036</td>\n",
       "      <td>0.00003</td>\n",
       "      <td>-0.005262</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               mkt     SMB     HML     RMW     CMA       RF       spy\n",
       "Date                                                                 \n",
       "2017-06-23  0.0024  0.0077 -0.0050 -0.0035  0.0005  0.00003 -0.000615\n",
       "2017-06-26  0.0004  0.0005  0.0069  0.0025  0.0017  0.00003  0.000738\n",
       "2017-06-27 -0.0084 -0.0025  0.0130  0.0031  0.0032  0.00003 -0.000779\n",
       "2017-06-28  0.0102  0.0082  0.0021 -0.0018 -0.0007  0.00003 -0.000205\n",
       "2017-06-29 -0.0083  0.0007  0.0135 -0.0025  0.0036  0.00003 -0.005262"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.concat([fama_5,spy],axis = 1).dropna()\n",
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                    spy   R-squared:                       0.340\n",
      "Model:                            OLS   Adj. R-squared:                  0.339\n",
      "Method:                 Least Squares   F-statistic:                     210.6\n",
      "Date:                Tue, 01 Aug 2017   Prob (F-statistic):          3.73e-110\n",
      "Time:                        16:03:03   Log-Likelihood:                 4457.8\n",
      "No. Observations:                1229   AIC:                            -8908.\n",
      "Df Residuals:                    1225   BIC:                            -8887.\n",
      "Df Model:                           3                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "Intercept   9.267e-05      0.000      0.503      0.615      -0.000       0.000\n",
      "mkt            0.5700      0.023     24.636      0.000       0.525       0.615\n",
      "SMB           -0.0753      0.039     -1.942      0.052      -0.151       0.001\n",
      "HML            0.0658      0.039      1.670      0.095      -0.011       0.143\n",
      "==============================================================================\n",
      "Omnibus:                      142.776   Durbin-Watson:                   2.657\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):              609.967\n",
      "Skew:                          -0.472   Prob(JB):                    3.53e-133\n",
      "Kurtosis:                       6.319   Cond. No.                         221.\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
     ]
    }
   ],
   "source": [
    "model = sm.ols(formula = 'spy ~ mkt+SMB+HML',data = df).fit()\n",
    "print model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                  small   R-squared:                       0.995\n",
      "Model:                            OLS   Adj. R-squared:                  0.995\n",
      "Method:                 Least Squares   F-statistic:                 9.912e+04\n",
      "Date:                Tue, 01 Aug 2017   Prob (F-statistic):               0.00\n",
      "Time:                        17:13:18   Log-Likelihood:                 8506.2\n",
      "No. Observations:                1517   AIC:                        -1.700e+04\n",
      "Df Residuals:                    1513   BIC:                        -1.698e+04\n",
      "Df Model:                           3                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "Intercept     -0.0001   2.29e-05     -6.254      0.000      -0.000   -9.82e-05\n",
      "mkt            1.0407      0.003    403.477      0.000       1.036       1.046\n",
      "SMB            0.8707      0.005    181.700      0.000       0.861       0.880\n",
      "HML            0.1050      0.005     21.744      0.000       0.095       0.114\n",
      "==============================================================================\n",
      "Omnibus:                       84.277   Durbin-Watson:                   1.984\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):              260.380\n",
      "Skew:                          -0.213   Prob(JB):                     2.88e-57\n",
      "Kurtosis:                       4.985   Cond. No.                         221.\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
     ]
    }
   ],
   "source": [
    "small_table = quandl.get('NASDAQOMX/NQUSS')\n",
    "small = np.log(small_table['Index Value']).diff().dropna()\n",
    "small.name = 'small'\n",
    "small_df = pd.concat([fama_5,small],axis = 1).dropna()\n",
    "small_model = sm.ols(formula = 'small~mkt+SMB+HML',data = small_df).fit()\n",
    "print small_model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                  large   R-squared:                       0.998\n",
      "Model:                            OLS   Adj. R-squared:                  0.998\n",
      "Method:                 Least Squares   F-statistic:                 2.425e+05\n",
      "Date:                Tue, 01 Aug 2017   Prob (F-statistic):               0.00\n",
      "Time:                        17:12:41   Log-Likelihood:                 9634.6\n",
      "No. Observations:                1517   AIC:                        -1.926e+04\n",
      "Df Residuals:                    1513   BIC:                        -1.924e+04\n",
      "Df Model:                           3                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "Intercept     -0.0001   1.09e-05    -11.144      0.000      -0.000   -9.98e-05\n",
      "mkt            0.9891      0.001    806.811      0.000       0.987       0.992\n",
      "SMB           -0.1468      0.002    -64.463      0.000      -0.151      -0.142\n",
      "HML           -0.0076      0.002     -3.305      0.001      -0.012      -0.003\n",
      "==============================================================================\n",
      "Omnibus:                       97.595   Durbin-Watson:                   1.959\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):              365.063\n",
      "Skew:                          -0.190   Prob(JB):                     5.34e-80\n",
      "Kurtosis:                       5.373   Cond. No.                         221.\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n"
     ]
    }
   ],
   "source": [
    "large_table = quandl.get('NASDAQOMX/NQUSL')\n",
    "large = np.log(large_table['Index Value']).diff().dropna()\n",
    "large.name = 'large'\n",
    "large_df = pd.concat([fama_5,large],axis = 1).dropna()\n",
    "large_model = sm.ols(formula = 'large~mkt+SMB+HML',data = large_df).fit()\n",
    "print large_model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
